/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : GetCompanyProducts
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Company Match Identifier
 */
class CompanyProducts{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid             = null;
	      string partnerPassword	       = null;
	      string companyMatchIdentifier    = null;
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid            = args[0].Trim();
	      partnerPassword          = args[1].Trim();
	      companyMatchIdentifier   = args[2].Trim();
	            
	      GraydonCompanyData graydonCompanyData = new GraydonCompanyData();
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                    = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                    = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                  = partnerPassword;
	      
	      // create the parameters
	      GetCompanyProducts_ParametersType getCompanyProducts_Parameters                            = new GetCompanyProducts_ParametersType();
	      getCompanyProducts_Parameters.Authentication_Parameters                                    = authentication_Parameters;
	      getCompanyProducts_Parameters.CompanyMatchIdentifier                                       = companyMatchIdentifier;

	      // Begin the Async call to "getCompanyProducts"
	      IAsyncResult ar = graydonCompanyData.BegingetCompanyProducts(getCompanyProducts_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      GetCompanyProducts_ResultType getCompanyProducts_Result = null;
	      
	      try
          {
             // get the results of the match
             getCompanyProducts_Result = graydonCompanyData.EndgetCompanyProducts(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }
          
         // get the service log details
         serviceLog(getCompanyProducts_Result.Service_Log);
               
         // get all the companies returned
         if(getCompanyProducts_Result.Company != null)
         {
           CompanyType company = getCompanyProducts_Result.Company;
           
           Console.WriteLine("Company Name\t\t: " + company.Name);
        
		   // get the returned company identifiers for the company
		   CompanyIdentifierType[] companyIdentifiers = company.CompanyIdentifiers; 
		   for (int y = 0; y < companyIdentifiers.Length; y++)
		   {
			   Console.WriteLine("Identifiers :");																	
			   Console.WriteLine("  Type\t\t\t: " + companyIdentifiers[y].Type);									    // output identifier type
			   Console.WriteLine("  Identifier\t\t: " + companyIdentifiers[y].Identifier);						        // output identifier
		   }
	
		   if (company.RegisteredAddress != null)
		   {
			   Console.WriteLine("Registered Address :");
			   if (company.RegisteredAddress.Building != null)
				   Console.WriteLine("  Building\t\t: " + company.RegisteredAddress.Building);			            // output the building
			   if (company.RegisteredAddress.Street != null)
				   Console.WriteLine("  Street\t\t: " + company.RegisteredAddress.Street);				            // output the street
			   if (company.RegisteredAddress.Town != null)
				   Console.WriteLine("  Town\t\t: " + company.RegisteredAddress.Town);					            // output the town
			   if (company.RegisteredAddress.City != null)
				   Console.WriteLine("  City\t\t\t: " + company.RegisteredAddress.City);				                // output the city
			   if (company.RegisteredAddress.County != null)
				   Console.WriteLine("  County\t\t: " + company.RegisteredAddress.County);				            // output the county
			   if (company.RegisteredAddress.PostCode != null)
				   Console.WriteLine("  PostCode\t\t: " + company.RegisteredAddress.PostCode);			            // output the postCode
			}
			
			if (company.TradingAddress != null)
			{
			   Console.WriteLine("Trading Address :");
			   if (company.TradingAddress.Building != null)
				   Console.WriteLine("  Building\t\t: " + company.TradingAddress.Building);				            // output the building
			   if (company.TradingAddress.Street != null)
				   Console.WriteLine("  Street\t\t: " + company.TradingAddress.Street);					            // output the street
			   if (company.TradingAddress.Town != null)
				   Console.WriteLine("  Town\t\t: " + company.TradingAddress.Town);						            // output the town
			   if (company.TradingAddress.City != null)
				   Console.WriteLine("  City\t\t\t: " + company.TradingAddress.City);		      			        // output the city
			   if (company.TradingAddress.County != null)
				   Console.WriteLine("  County\t\t: " + company.TradingAddress.County);					            // output the county
			   if (company.TradingAddress.PostCode != null)
				   Console.WriteLine("  PostCode\t\t: " + company.TradingAddress.PostCode);				            // output the postCode
			}
			
			if (company.DateOfIncorporation != null)
				Console.WriteLine("Incorporation Date\t: " + "{0:d}",company.DateOfIncorporation);	                // output date of incorporation
			if (company.DateAccountsFiled != null)
				Console.WriteLine("Accounts Filed Date\t: " + "{0:d}",company.DateAccountsFiled);	                // output date of accounts filed
			if (company.DateOfAnnualReturn != null)
				Console.WriteLine("Annual Return Date\t: " + "{0:d}",company.DateOfAnnualReturn);	                // output date of annual return
			if (company.DateOfLatestAccounts != null)
				Console.WriteLine("Latest Accounts Date\t: " + "{0:d}",company.DateOfLatestAccounts);               // output date of latest accounts
			if (company.Telephone != null)
				Console.WriteLine("Telephone\t\t: " + company.Telephone);										    // output the telephone number
			if (company.Email != null)
				Console.WriteLine("Email\t\t\t: " + company.Email);											        // output the email address
			if (company.Facsimile != null)
				Console.WriteLine("Fax\t\t\t: " + company.Facsimile);											    // output the fax address
			if (company.CompanyMatchIdentifier != null)
				Console.WriteLine("Company Match Identifier: " + company.CompanyMatchIdentifier);			        // output the company match identifier
				
			// get the returned company identifiers for the company
			if (company.Activites != null)
			{
				ActivityType[] activities = company.Activites; 
				for (int y = 0; y < activities.Length; y++)
				{
					Console.WriteLine("Activities :");																	
					Console.WriteLine("  Type\t\t\t: " + activities[y].Type);											// output activity type
					Console.WriteLine("  Description\t\t: " + activities[y].Description);								// output acitivity description
				}
			}
			
			Console.WriteLine(" ");
			Console.WriteLine("Products:");	
			
			// get the returned company identifiers for the company
			if (getCompanyProducts_Result.Products != null)
			{
				ProductType[] products 	= getCompanyProducts_Result.Products;
				for (int y = 0; y < products.Length; y++)
				{
					Console.WriteLine("Product :");	
					Console.WriteLine("  Code\t\t\t: " + products[y].Code);										      // output product code
					Console.WriteLine("  Type\t\t\t: " + products[y].Type);										      // output product type
					Console.WriteLine("  Level\t\t\t: " + products[y].Level);										  // output product level
					Console.WriteLine("  Service Speed\t\t: " + products[y].ServiceSpeed);						      // output product service speed
					Console.WriteLine("  Days From\t\t: " + products[y].DaysFrom);								      // output product days from
					Console.WriteLine("  Days To\t\t: " + products[y].DaysFrom);									  // output product days to
					Console.WriteLine("  Company Product Identifier: " + products[y].CompanyProductIdentifier);	      // output product company product identifier
					
					// get the returned company identifiers for the company
					if (products[y].MimeTypes != null)
					{
						Console.WriteLine("  MimeTypes Available :");
						MimeTypesType[] mimeTypes = products[y].MimeTypes; 
						for (int z = 0; z < mimeTypes.Length; z++)
						{																	
							Console.WriteLine("    Type\t\t: " + mimeTypes[z]);								           // output mime type
						}
					}
					Console.WriteLine(" ");
				}
			}
		 }
	  }  
      catch (Exception e)
      {
	     Console.WriteLine("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}
   
}